package com.dhecp.project.purchase.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.core.utils.ReflectUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.domain.ModuleNID;
import com.dhecp.project.common.domain.ModuleNidAndBid;
import com.dhecp.project.common.domain.RecordCheck;
import com.dhecp.project.common.domain.UpdateDeleted;
import com.dhecp.project.common.domain.UpdateFlow;
import com.dhecp.project.common.mapper.ModuleNidAndBidMapper;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.purchase.domain.OfflinePayment;
import com.dhecp.project.purchase.domain.OfflinePurchaseOrder;
import com.dhecp.project.purchase.domain.PurchasePlan;
import com.dhecp.project.purchase.mapper.OfflinePaymentMapper;
import com.dhecp.project.purchase.mapper.OfflinePurchaseOrderMapper;
import com.dhecp.project.purchase.mapper.PurchasePlanMapper;
import com.dhecp.project.purchase.service.OfflinePurchaseOrderService;
import com.dhecp.project.storage.domain.PurchaseStorage;
import com.dhecp.project.storage.mapper.PurchaseStorageMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 线下采购订单
 * @author: hlf
 * @create: 2022-10-28 16:43
 */
@Service
public class OfflinePurchaseOrderServiceImpl extends BaseModuleServiceImpl implements OfflinePurchaseOrderService {

    @Autowired
    private OfflinePurchaseOrderMapper purchaseMapper;
    @Autowired
    private PurchasePlanMapper purchasePlanMapper;
    @Autowired
    private PurchaseStorageMapper storageMapper;
    @Autowired
    private OfflinePaymentMapper offlinePaymentMapper;


    @Autowired
    private ModuleNidAndBidMapper moduleNidAndBidMapper;
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     * @Description: 查询前一单NID、BID
     * @Param: [params] nid 必要参数：当前 单据编码
     * @return: com.dhecp.common.core.domain.AjaxResult  (data.nid 前一单NID， data.bid 前一单BID)
     * @Author: hlf
     * @date: 2022-05-09 10:08
     */
    @Override
    public AjaxResult queryLastNid(Map<String, Object> params) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (params.get("nid") == null || params.get("nid") == "") {
            params.put("nid", "0");
        }
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        ModuleNID moduleNID = purchaseMapper.selectLastNid(params);
        if (moduleNID == null) {
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfirstnid"), TypeUtils.mapToJson(dataMap).toString());
        }
        dataMap.put("nid", moduleNID.getNid().toString());
        dataMap.put("bid", moduleNID.getBid().toString());
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), TypeUtils.mapToJson(dataMap).toString());
    }


    /**
     * @Description: 查询后一单NID、BID
     * @Param: [params] nid 必要参数：当前 单据编码
     * @return: com.dhecp.common.core.domain.AjaxResult  (data.nid 后一单NID， data.bid 后一单BID)
     * @Author: hlf
     * @date: 2022-05-09 10:09
     */
    @Override
    public AjaxResult queryNextNid(Map<String, Object> params) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (params.get("nid") == null || params.get("nid") == "" || "0".equals(params.get("nid").toString())) {
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfinallynid"), TypeUtils.mapToJson(dataMap).toString());
        }
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        ModuleNID moduleNID = purchaseMapper.selectNextNid(params);
        if (moduleNID == null) {
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfinallynid"), TypeUtils.mapToJson(dataMap).toString());
        }
        dataMap.put("nid", moduleNID.getNid().toString());
        dataMap.put("bid", moduleNID.getBid().toString());
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), TypeUtils.mapToJson(dataMap).toString());
    }


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.hr.domain.OfflinePurchaseOrder
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public OfflinePurchaseOrder queryFirstRecord(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return purchaseMapper.selectFirstRecordByNid(params);
    }

    /**
     * @Description: 查询指定单nid的整单录
     * @Param: [params] nid 必要参数：当前 单据编码
     * @return: java.util.List<com.dhecp.project.hr.domain.OfflinePurchaseOrder>
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public List<OfflinePurchaseOrder> queryListByNid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return purchaseMapper.selectByNid(params);
    }


    /**
     * @Description: 查询
     * @Param: [params]
     * @return: java.util.List<com.dhecp.project.hr.domain.OfflinePurchaseOrder>
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public List<OfflinePurchaseOrder> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));    //常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        PageUtils.startPageStronger();
        if (params.containsKey("orPay") && params.get("orPay").equals("是")){
            params.put("wherestr",params.get("wherestr")+"and (t1.IsPay <> '已付款' and (t1.cost2 - t1.AmountPaid) > 0)");
        }else if (params.containsKey("state") && params.get("state").equals("未入库")){
            params.put("wherestr",params.get("wherestr")+"and (t1.ninqty > 0)");
        }
        return purchaseMapper.select(params);
    }


    /**
     * @Description: 查询已选择的数据
     * @Param: [params]
     * @return: java.util.List<com.dhecp.project.system.domain.SysUserInfoSimple>
     * @Author: hlf
     * @date: 2022-08-18 17:14
     */
    @Override
    public List<OfflinePurchaseOrder> querySelectList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));        //常规查询条件SQL语句
        if (params.containsKey("selIdList")) {
            params.put("selIdList", params.get("selIdList").toString().split(","));
        }
        if (params.get("field") == null || params.get("field") == "") {
            String orderBy = "bid asc";
            PageUtils.startPageOrder(orderBy);
        } else {
            PageUtils.startPage();
        }
        return purchaseMapper.querySelectList(params);
    }


    /**
     * @Description: 根据母件获取之间 (多条
     * @Param: [fBm]
     * @return: java.util.List<com.dhecp.project.business.domain.ProductBasisComponent>
     * @Author: hlf
     * @date: 2022-08-18 17:14
     */
    @Override
    public List<OfflinePurchaseOrder> querySonListByPNid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));        //常规查询条件SQL语句
        if (params.get("field") == null || params.get("field") == "") {
            String orderBy = "nid asc";
            PageUtils.startPageOrder(orderBy);
        } else {
            PageUtils.startPage();
        }
        return purchaseMapper.querySonListByPNid(params);
    }

    /**
     * @Description: 根据母件获取子表 (多条
     * @Param: [fBm]
     * @return: java.util.List<com.dhecp.project.business.domain.ProductBasisComponent>
     * @Author: hlf
     * @date: 2022-08-18 17:14
     */
    @Override
    public List<OfflinePurchaseOrder> querySonListByBID(Map<String, Object> params) throws Exception {
        if (params.get("field") == null || params.get("field") == "") {
            String orderBy = "t2.bid asc";
            PageUtils.startPageOrder(orderBy);
        } else {
            PageUtils.startPage();
        }
        return purchaseMapper.querySonListByBID(params);
    }


    /**
     * @Description: 保存
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult saveData(Map<String, Object> params) {
        params.put("businessEntity", new OfflinePurchaseOrder());

        AjaxResult ajaxResult = this.saveDataBase(params);
        if (!"0".equals(ajaxResult.get("code").toString())) {
            throw new RuntimeException(ajaxResult.get("msg").toString());
        }

        String resNid = "0";
        Map<String, Object> infoMap = (Map<String, Object>) ajaxResult.get("infoMap");
        List<OfflinePurchaseOrder> businessDataList = (List<OfflinePurchaseOrder>) ajaxResult.get("businessDataList");

        List<RecordCheck> guidMapList = purchaseMapper.findRecords(infoMap.get("nid").toString());
        Map<String, Object> guidMap = new HashMap<String, Object>();
        if (!guidMapList.isEmpty()) {
            if (infoMap.get("newNid") != null && "1".equals(infoMap.get("newNid").toString())) { //单号重复验证
                throw new RuntimeException(MessageUtils.message("module.error.retry"));
            }
            for (RecordCheck recordCheck : guidMapList) {
                if (recordCheck != null && recordCheck.getGuid() != null) {
                    guidMap.put(recordCheck.getGuid().toString(), "1");
                }
            }
        }

        if (businessDataList == null || businessDataList.size() == 0) {
            resNid = "0";
        } else {
            resNid = infoMap.get("nid").toString();
            for (OfflinePurchaseOrder businessData : businessDataList) {
                String guid = "";
                if (businessData.getGuid() == null) {
                    guid = RandomValueUtils.getGUID();
                    businessData.setGuid(guid);
                } else {
                    guid = businessData.getGuid().toString();
                }

                for (String field : infoMap.keySet()) { //遍历非网格字段，并赋值到实体中
                    Object fieldValue = infoMap.get(field);
                    ReflectUtils.setValue(businessData, field, fieldValue);
                }

                if (guidMap.get(guid) == null) {
                    businessData.setIsPay("未付款");
                    businessData.setState("未入库");
                    businessData.setInqty(0);
                    businessData.setNinqty(businessData.getSum());
                    businessData.setAmountPaid(0.00);
                    businessData.setUnpaidAmount(businessData.getOrderAmount());
                    purchaseMapper.insert(businessData);
                } else {
                    purchaseMapper.update(businessData);
                    guidMap.remove(guid);
                }

            }

        }

        if (!guidMap.isEmpty()) {
            for (String guid : guidMap.keySet()) {
                purchaseMapper.delete(guid);
            }
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), resNid);
    }

    /**
     * @Description: 删除
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<OfflinePurchaseOrder> businessDataList = JSON.parseArray(params.get("infoItems").toString(), OfflinePurchaseOrder.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (OfflinePurchaseOrder businessData : businessDataList) {
            purchaseMapper.delete(businessData);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     * @Description: 修改删除标志（预留）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串（仅需 guid 和 deleted）
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult updateDeleted(Map<String, Object> params) {
        List<UpdateDeleted> updateDeletedList = JSON.parseArray(params.get("infoItems").toString(), UpdateDeleted.class);
        if (updateDeletedList == null || updateDeletedList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (UpdateDeleted updateDeleted : updateDeletedList) {
            purchaseMapper.updateDeleted(updateDeleted);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     * @Description: 流转  —— 添加流程任务、修改流程状态及生效标志
     * @Param: [params] tableDataId 必要参数：单据编码（nid）
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult updateFlowGo(Map<String, Object> params) {
        Map<String, Object> infoMap = new HashMap<String, Object>();
        String nid = "0";
        if (params.get("tableDataId") == null || params.get("tableDataId") == "") {
            return AjaxResult.error(MessageUtils.message("module.error.lack", "单据编码【tableDataId】"));
        } else {
            nid = params.get("tableDataId").toString();
        }
        params.put("nid", nid);
        List<OfflinePurchaseOrder> businessDataList = purchaseMapper.selectByNid(params);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.nodata"));
        } else {
            infoMap.put("cid", businessDataList.get(0).getCid());
            infoMap.put("bUser", businessDataList.get(0).getbUser());
            infoMap.put("bDept", businessDataList.get(0).getbDept());
            infoMap.put("procInstId", businessDataList.get(0).getProcInstId());
            infoMap.put("activeFlow", businessDataList.get(0).getActiveFlow());
            infoMap.put("createTime", businessDataList.get(0).getCreateTime());
            params.put("infoMap", TypeUtils.mapToJson(infoMap).toString());
            params.put("activeFlowOld", businessDataList.get(0).getActiveFlow());
        }

        //流程处理公共方法，返回单据（流转、回退等）后的流程状态信息
        if (params.get("optType") == null || params.get("optType") == "") {
            params.put("optType", "go");
        }
        UpdateFlow updateFlow = new UpdateFlow();
        updateFlow = this.FlowDealBase(params);

        for (OfflinePurchaseOrder businessData : businessDataList) {
            if (updateFlow.getActiveFlow().equals("end")) {//当流程结束时
                Map<String, Object> infoMap2 = new HashMap<String, Object>();
                infoMap2.put("bid",businessData.getPlanID());
                List<PurchasePlan> purchasePlanList = purchasePlanMapper.findByIdList(infoMap2);
                for (PurchasePlan item:purchasePlanList
                     ) {
                    item.setOrderState("订单已生成");
                    purchasePlanMapper.update(item);
                }

            }
            updateFlow.setGuid(businessData.getGuid().toString());
            purchaseMapper.updateFlow(updateFlow);
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), nid);
    }

    /**
     * @Description: 退回  —— 添加流程任务、修改流程状态及生效标志
     * @Param: [params] tableDataId 必要参数：单据编码（nid）
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult updateFlowBack(Map<String, Object> params) {
        Map<String, Object> infoMap = new HashMap<String, Object>();
        String nid = "0";
        if (params.get("tableDataId") == null || params.get("tableDataId") == "") {
            return AjaxResult.error(MessageUtils.message("module.error.lack", "单据编码【tableDataId】"));
        } else {
            nid = params.get("tableDataId").toString();
        }
        params.put("nid", nid);
        List<OfflinePurchaseOrder> businessDataList = purchaseMapper.selectByNid(params);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.nodata"));
        } else {
            infoMap.put("cid", businessDataList.get(0).getCid());
            infoMap.put("bUser", businessDataList.get(0).getbUser());
            infoMap.put("bDept", businessDataList.get(0).getbDept());
            infoMap.put("procInstId", businessDataList.get(0).getProcInstId());
            infoMap.put("activeFlow", businessDataList.get(0).getActiveFlow());
            infoMap.put("createTime", businessDataList.get(0).getCreateTime());
            params.put("infoMap", TypeUtils.mapToJson(infoMap).toString());
            params.put("activeFlowOld", businessDataList.get(0).getActiveFlow());
        }

        //流程处理公共方法，返回单据（流转、回退等）后的流程状态信息
        if (params.get("optType") == null || params.get("optType") == "") {
            params.put("optType", "back");
        }
        UpdateFlow updateFlow = new UpdateFlow();
        updateFlow = this.FlowDealBase(params);

        for (OfflinePurchaseOrder businessData : businessDataList) {
            updateFlow.setGuid(businessData.getGuid().toString());
            purchaseMapper.updateFlow(updateFlow);
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), nid);
    }


    /**
     * 修改审核状态
     *
     * @param infoItems
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult updateType(Map<String, Object> params) {
        List<OfflinePurchaseOrder> offlinePurchaseOrders = JSON.parseArray(params.get("infoItems").toString(), OfflinePurchaseOrder.class);
        for (OfflinePurchaseOrder item : offlinePurchaseOrders
        ) {
//            item.setState(params.get("vType").toString());
            item.setUpdateTime(new Date());
            purchaseMapper.update(item);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     * 修改物流信息
     *
     * @param infoItems
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult saveLogistics(Map<String, Object> params) {
        try {
            OfflinePurchaseOrder offlinePurchaseOrder = JSON.parseObject(params.get("infoItems").toString(), OfflinePurchaseOrder.class);
            if (offlinePurchaseOrder != null) {
                OfflinePurchaseOrder order = purchaseMapper.findByGuid(offlinePurchaseOrder.getGuid());



                if (order.getIsPay().equals("已付款")) {
                    return AjaxResult.error("已付款的订单无法进行修改！");
                }else{
                    //新付款总数
                    double cost = offlinePurchaseOrder.getPrice2()*offlinePurchaseOrder.getSum();
                    //旧付款总数
                    double oldCost2 = order.getCost2();
                    //新采购数量
                    int sum = offlinePurchaseOrder.getSum();

                    if (cost < order.getAmountPaid()){
                        offlinePurchaseOrder.setUnpaidAmount(cost-order.getAmountPaid());
                        return AjaxResult.error("付款总数不可小于已付款金额！");
                    }else if (sum < order.getInqty()){
                        return AjaxResult.error("采购数量不可小于已到货数量！");
                    }else{
                        offlinePurchaseOrder.setCost2(cost);
                        purchaseMapper.update(offlinePurchaseOrder);
                    }

                    //获取线下入库单数据
//                    OfflinePurchaseStorage offlinePurchaseStorage = offlinePurchaseStorageMapper.findByPGuid(offlinePurchaseOrder.getGuid());
//                    if (offlinePurchaseStorage!=null){
//                        offlinePurchaseStorage.setsNum(offlinePurchaseOrder.getSum());
//                        offlinePurchaseStorage.setNinqty(offlinePurchaseOrder.getNinqty());
//                        offlinePurchaseStorageMapper.update(offlinePurchaseStorage);
//                    }
                    //获取线下付款单数据

                    Map<String, Object> params2 = new HashMap<String, Object>();
                    params2.put("nid", offlinePurchaseOrder.getNid());
                    List<OfflinePurchaseOrder> offlinePurchaseOrders = purchaseMapper.selectByNid(params2);
                    double oldCostAllSum = 0;//旧订单总金额
                    for (OfflinePurchaseOrder item : offlinePurchaseOrders) {
                        oldCostAllSum += item.getPrice2() * item.getSum();
                    }

                    //修改订单总金额
                    oldCostAllSum = oldCostAllSum - oldCost2 + cost;
                    for (OfflinePurchaseOrder item : offlinePurchaseOrders) {
                        item.setOrderAmountAll(oldCostAllSum);
                        item.setUnpaidAmount(item.getSum()*item.getPrice2());
                        purchaseMapper.update(item);

                        List<OfflinePayment> offlinePayment = offlinePaymentMapper.findBypGuid(item.getGuid());
                        for (OfflinePayment item2:offlinePayment
                             ) {
                            if (item2!=null){
                                item2.setPrice2(item.getPrice2());
                                item2.setSum(item.getSum());
                                item2.setCost2(item.getSum()*item.getPrice2());
                                item2.setUnpaidAmount(item.getSum()*item.getPrice2()-item2.getAmountPaid());
                                offlinePaymentMapper.update(item2);
                            }
                        }
                    }

//                    //修改线下付款单 金额
//                    List<OfflinePayment> offlinePaymentList = offlinePaymentMapper.findByOrderID(offlinePurchaseOrder.getBid());
//                    for (OfflinePayment item:offlinePaymentList
//                         ) {
//                        item.setUnpaidAmount(0.00);
//                    }



                }

            }


        } catch (Exception e) {
            return  AjaxResult.error(e.getCause().getMessage());
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    // 线下订单 生成采购入库单  -- 停用 不走这个了  --230321
    public void getPurchaseStorageData(OfflinePurchaseOrder offlinePurchaseOrder) {
        PurchaseStorage purchaseStorage = new PurchaseStorage();//创建新的采购入库单
        Map<String, Object> params2 = new HashMap<>();

        try {
            //判断是否存在入库单
            List<PurchaseStorage> purchaseStorages = storageMapper.selectPlanID3(offlinePurchaseOrder.getPlanID(),offlinePurchaseOrder.getsCode());
            if (purchaseStorages.size() < 1) {
                params2.put("moduleTable", "TCG_PURCHASESTORAGE");
                params2.put("entity", new PurchaseStorage()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
                ModuleNidAndBid moduleNidAndBid = moduleNidAndBidMapper.findModuleNidAndBid(params2);
                purchaseStorage.setBid(moduleNidAndBid.getBid());
                purchaseStorage.setNid(moduleNidAndBid.getNid());
                purchaseStorage.setGuid(RandomValueUtils.getGUID());
                purchaseStorage.setCreateTime(new Date());
                purchaseStorage.setCid(ShiroUtils.getMandatorCid());
                purchaseStorage.setCidName(ShiroUtils.getMandatorCidName());
                purchaseStorage.setbUser(ShiroUtils.getMandatorId());
                purchaseStorage.setbUserName(ShiroUtils.getMandatorName());
                purchaseStorage.setbDept(ShiroUtils.getMandatorDeptPath());
                purchaseStorage.setbDeptName(ShiroUtils.getMandatorDeptName());
                purchaseStorage.setaUser(ShiroUtils.getUserId());
                purchaseStorage.setaUserName(ShiroUtils.getUserName());
                purchaseStorage.setaDept(ShiroUtils.getUserDeptPath());
                purchaseStorage.setaDeptName(ShiroUtils.getUserDeptName());

                purchaseStorage.setvGsBm(offlinePurchaseOrder.getvGsBm());
                purchaseStorage.setvGsDz(offlinePurchaseOrder.getvGsDz());
                purchaseStorage.setvGsJc(offlinePurchaseOrder.getvGsJc());
                purchaseStorage.setvGsMc(offlinePurchaseOrder.getvGsMc());

                purchaseStorage.setPlanID(offlinePurchaseOrder.getPlanID());//计划单号
                purchaseStorage.setOrderID(offlinePurchaseOrder.getBid());//订单单号
                purchaseStorage.setOrderGuid(offlinePurchaseOrder.getGuid());//订单标识
                purchaseStorage.setOrderAddress(offlinePurchaseOrder.getOrderAddress());//采购下单地址
//                purchaseStorage.setAssemblyPlan(businessData.getPlanID());//组装方案
                purchaseStorage.setpCode(offlinePurchaseOrder.getCode());//商品编码
                purchaseStorage.setpName(offlinePurchaseOrder.getName());//商品名称
                purchaseStorage.setCode(offlinePurchaseOrder.getCode());//商品编码
                purchaseStorage.setName(offlinePurchaseOrder.getName());//商品名称
                purchaseStorage.setTrackingNumber(offlinePurchaseOrder.getAliLogisticsBillNo());//物流单号
                purchaseStorage.setPlace(offlinePurchaseOrder.getAliPlace());//物流渠道
                purchaseStorage.setsCode(offlinePurchaseOrder.getsCode());//子件编码
                purchaseStorage.setsName(offlinePurchaseOrder.getsName());//子件名称
                purchaseStorage.setsNum(offlinePurchaseOrder.getSum());//子件数量
                purchaseStorage.setsPrice(offlinePurchaseOrder.getPrice2());//子件单价
                purchaseStorage.setsSum(offlinePurchaseOrder.getCost2());//子件金额
                purchaseStorage.setState("未入库");//入库状态
                purchaseStorage.setvZzZt("未组装");//组装状态
                purchaseStorage.setIsDyRk("未打印");//组装状态
                purchaseStorage.setSource("线下");//数据来源
                storageMapper.insert(purchaseStorage);//生成入库单
                offlinePurchaseOrder.setWarehouse(purchaseStorage.getBid());
                offlinePurchaseOrder.setWarehouseTime(new Date());
//                businessData.setIsPay("已付款");
                purchaseMapper.update(offlinePurchaseOrder);
                PurchasePlan purchasePlan = purchasePlanMapper.findByBID(purchaseStorage.getPlanID());//修改采购计划的订单状态
                purchasePlan.setOrderState("订单已生成");
                purchasePlanMapper.updateOrderState(purchasePlan);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
